package Reaction::Role;

use Moose::Role ();
use Reaction::ClassExporter;
use Reaction::Class;
use Moose::Meta::Class;

*Moose::Meta::Role::add_method = sub {
  Moose::Meta::Class->can("add_method")->(@_);
};

class Role which {

  override exports_for_package => sub {
    my ($self, $package) = @_;
    my %exports = $self->SUPER::exports_for_package($package);
    delete $exports{class};
    $exports{role} = sub { $self->do_role_sub($package, @_); };
    return %exports;
  };
  
  override next_import_package => sub { 'Moose::Role' };
  
  override default_base => sub { () };

  implements do_role_sub => as {
    my ($self, $package, $role, $which, $setup) = @_;
    confess "Invalid role declaration, should be: role Role which { ... }"
      unless ($which eq 'which' && ref($setup) eq 'CODE');
    $self->setup_and_cleanup($role, $setup);
  };

};
  
1;
